﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace OF.Web
{
    public class WebUtils
    {
        #region 打印到页面

        public void PrintResult(string result, bool isencrypt)
        {
            if (isencrypt)
            {
                string s = EncryptUtil.Encrypt(result);
                HttpContext.Current.Response.Write(s);
            }
            else
            {
                HttpContext.Current.Response.Write(result);
            }
        }

        #endregion

        #region 获取参数

        public static string GetPara(string para, bool isencrypt)
        {
            string val = string.Empty;
            try
            {
                val = Common.OFRequest.GetFormString(para);
                if (string.IsNullOrEmpty(val))
                {
                    val = Common.OFRequest.GetQueryString(para);
                }
                val = val.Replace('-', '+');
                if (isencrypt) val = EncryptUtil.Decrypt(val);   //当参数加密则需要解密
               // val = Common.SqlKey.CheckWord(val);
            }
            catch (Exception ex)
            {
                Common.LogHelper.WriteLog(ex.ToString(), "GetPara获取参数");
            }
            return val;
        }

        #endregion

        #region 保存卖家登陆信息
        public static void AddSession(string name, string id)
        {
            HttpContext.Current.Session["admin"] = name + "&" + id;
        }
        #endregion

        #region 保存买家登陆信息
        public static void AddBuyerSession(string name, string id)
        {
            HttpContext.Current.Session["buyer"] = name + "&" + id;
        }
        #endregion

        #region 获取信息
        /// <summary>
        /// 返回指定索引的值
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        static string GetAdminInfo(int index)
        {
            if (IsLogin())
            {
                string[] str = HttpContext.Current.Session["admin"].ToString().Split('&');
                if (str.Length == 2)
                {
                    return HttpContext.Current.Server.UrlDecode(str[index]);
                }
                else
                {
                    HttpContext.Current.Session["admin"] = null;
                    return "0";
                }
            }
            else
            {
                HttpContext.Current.Response.Redirect("/admin/login.aspx");
                return "-1";
            }

        }
        #endregion

        #region 获取id
        public static int GetId()
        {
            return Convert.ToInt32(GetAdminInfo(1));
        }
        #endregion

        #region 获取用户名
        public static string GetName()
        {
            return GetAdminInfo(0);
        }
        #endregion

        #region 验证登陆信息
        public static bool IsLogin()
        {
            if (HttpContext.Current.Session["admin"] == null)
                return false;
            else
                return true;
        }
        #endregion

        #region 删除登陆信息
        public static void DeleteSession()
        {
            HttpContext.Current.Session["admin"] = null;
        }
        #endregion


        #region 获取买家信息
        /// <summary>
        /// 返回指定索引的值
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        static string GetBuyerInfo(int index)
        {
            if (IsBuyerLogin())
            {
                string[] str = HttpContext.Current.Session["buyer"].ToString().Split('&');
                if (str.Length == 2)
                {
                    return HttpContext.Current.Server.UrlDecode(str[index]);
                }
                else
                {
                    HttpContext.Current.Session["buyer"] = null;
                    return "0";
                }
            }
            else
            {
                HttpContext.Current.Response.Redirect("/user/login.aspx");
                return "-1";
            }

        }
        #endregion

        #region 获取id
        public static int GetBuyerId()
        {
            return Convert.ToInt32(GetBuyerInfo(1));
        }
        #endregion

        #region 获取用户名
        public static string GetBuyerName()
        {
            return GetBuyerInfo(0);
        }
        #endregion

        #region 验证登陆信息
        public static bool IsBuyerLogin()
        {
            if (HttpContext.Current.Session["buyer"] == null)
                return false;
            else
                return true;
        }
        #endregion

        #region 删除登陆信息
        public static void DeleteBuyerSession()
        {
            HttpContext.Current.Session["buyer"] = null;
        }
        #endregion


        #region 买家常用学校地址缓存
        /// <summary>
        /// 买家常用学校地址缓存
        /// </summary>
        /// <param name="schoolid"></param>
        public static void AddSchoolCookie(string schoolid)
        {
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("schoolid", schoolid));
            HttpContext.Current.Response.Cookies["schoolid"].Expires = DateTime.MaxValue;
         
        }
        #endregion

        #region 获取买家常用学校地址缓存
        /// <summary>
        /// 获取买家常用学校地址缓存
        /// </summary>
        /// <param name="schoolid"></param>
        public static string GetSchoolCookie()
        {
            if (HttpContext.Current.Request.Cookies["schoolid"] == null)
                return string.Empty;
            return HttpContext.Current.Request.Cookies["schoolid"].Value;
            
        }
        #endregion

    }
}